/*
Copyright (c) 2007, Husted dot Com, Inc. All rights reserved.
Portions Copyright (c) 2007, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 0.0.1
*/
/**
 * The ANVIL object is the single global object used by the Anvil JavaScript 
 * library. Like some other Anvil classes, ANVIL is based on a corresponding 
 * YUI class, YAHOO.
 * 
 * @module anvil
 * @requires remedial
 * @title  ANVIL Global
 */
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/

if (typeof ANVIL == "undefined") {
    /**
     * The ANVIL global namespace object.  If ANVIL is already defined, the
     * existing ANVIL object will not be overwritten so that defined
     * namespaces are preserved.
     * @class ANVIL
     * @static
     */
    var ANVIL = {};
}

/**
 * Returns the namespace specified and creates it if it doesn't exist
 * <pre>
 * ANVIL.namespace("property.package");
 * ANVIL.namespace("ANVIL.property.package");
 * </pre>
 * Either of the above would create ANVIL.property, then
 * ANVIL.property.package
 *
 * Be careful when naming packages. Reserved words may work in some browsers
 * and not others. For instance, the following will fail in Safari:
 * <pre>
 * ANVIL.namespace("really.long.nested.namespace");
 * </pre>
 * This fails because "long" is a future reserved word in ECMAScript
 *
 * @method namespace
 * @static
 * @param  {String*} arguments 1-n namespaces to create 
 * @return {Object}  A reference to the last namespace object created
 */
ANVIL.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=ANVIL;

        // ANVIL is implied, so it is ignored if it is included
        for (j=(d[0] == "ANVIL") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }

    return o;
};


/**
 * Initializes the global by creating the default namespaces and applying
 * any new configuration information that is detected.
 * @method init
 * @static
 */
ANVIL.init = function() {
    this.namespace("widget");
}
ANVIL.init();

ANVIL.messageId = "ANVIL.message";

ANVIL.detailId = "ANVIL.detail";

ANVIL.clearMessage = function () {
    top.document.getElementById(ANVIL.messageId).innerHTML = "<p>&nbsp;</p>";    
    top.document.getElementById(ANVIL.detailId).innerHTML = "<p>&nbsp;</p>";    
};

ANVIL.error = function (sMessage, sDetail) {    
    // var sTemplate1 = "<table><tr><th valign=top>Message:&nbsp;</th><td>{message}</td></tr>";
    var sMessageTemplate = "<p>{message}</p>";
    var sMessage = sMessageTemplate.supplant({message: sMessage});
    top.document.getElementById(ANVIL.messageId).innerHTML = sMessage;
    if (!sDetail) sDetail = "&nbsp;" ;
    // var sTemplate2 = "<tr><th valign=top>Location:</th><td>{stackTrace}</td></tr></table>";
    var sDetailTemplate = "<p>{detail}</p>";
    var sDetail = sDetailTemplate.supplant({detail: sDetail});
    alert("Error Communicating with Server! See message area for details.");  
};

ANVIL.setMessage = function (sMessage) {
    // var sTemplate = "<table><tr><th valign=top>Message:&nbsp;</th><td>{message}</td></tr></table>";
    var sMessageTemplate = "<p>{message}</p>";    
    var oContext = {message:sMessage};
    top.document.getElementById(ANVIL.messageId).innerHTML = sMessageTemplate.supplant(oContext);
};

ANVIL.loading = function() {
    ANVIL.setMessage("Loading ...");
}

ANVIL.channelError = function (o) {
    var sPayload =  eval("(" + o + ")") ; // JSON.eval(o);
    error(sPayload.error.message,sPayload.error.stackTrace);
};

ANVIL.channel = function (fnCaller) {
    var result = ANVIL.yui.channel (fnCaller);
    return result;
};
